package com.scaffold.authority.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;

public class BasicBizDAO<T, ID extends Serializable> extends com.scaffold.springmvc.dao.BasicBizDAO<T, ID> {
	
	public List<T> findByIdList(Collection<ID> idList){		
		List<T> result = new ArrayList<T>();
		for(ID id:idList){
			result.add(this.findById(id));
		}
		return result;
	}

	protected T findUniqueByCriteria(DetachedCriteria dcriteria) {
		Criteria c = dcriteria.getExecutableCriteria(this.getSession());
		List<T> list = c.list();
		return list.get(0);
	}
	
	public int deleteByProperty(String propertyName, Object propertyValue){
		if(!Pattern.matches("^\\w+$", propertyName)){
			return 0;
		}
		StringBuilder sb = new StringBuilder();
		sb.append("delete from "+this.persistentClass.getSimpleName());
		sb.append(" where ");
		sb.append(propertyName+"=?");
		return this.executeUpdate(sb.toString(),propertyValue);
	}
}
